// pages/settle/settle.js
var config = require('../../config.js');
var request = require('../../utils/request.js');
var appkey = require('../../appkey.js');
var app = getApp();
var payutil = require('../../utils/pay.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    goodsLst: [],
    origCost: 0,
    address_id: '',
    address: undefined,
    activityId: undefined,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.activityid) {
      this.setData({
        activityId: options.activityid
      });
      this.getActivityOrderInfo();
    } else {
      this.getOrderInfoByCarAction();
    }
    this.getAddressLst();
  },

  getActivityOrderInfo: function () {
    let url = config.activityOrderInfoUrl;
    var shopid = app.globalData.shopId;
    let params = {
      shopId: shopid,
      activityId: this.data.activityId
    }

    request.requestPost(url, params)
      .then(data => {
        console.log(data);
        this.setData({
          goodsLst: data.goodsList,
          origCost: data.origCost
        });
      }).catch(msg => {
      });
  },

  /**
   * 购物车下单订单信息
   */
  getOrderInfoByCarAction: function () {
    var url = config.getOrderInfoUrl;
    var shopkey = wx.getStorageSync(appkey.shoppingkey);
    var shopid = app.globalData.shopId;
    var params = {
      spk: shopkey,
      shopId: shopid
    }
    console.log(params);
    request.requestPost(url, params)
      .then(data => {
        this.setData({
          goodsLst: data.goodsList,
          origCost: data.origCost
        });
      }).catch(msg => {
        console.log(msg);
      })
  },

  chooseAddress: function () {
    wx.navigateTo({
      url: '../address/addresslist/addresslist?from=settle',
    });
  },

  /**
   * 创建订单
   * spk	购物车key	否	购物车结算必传
   * shopId	商家id	否	商家id
   * addressId	地址id	否
   * payType	支付类别	否	支付类别
   */
  createTrade: function () {
    var url = config.carOrderPlaceUrl;
    var shopkey = wx.getStorageSync(appkey.shoppingkey);
    var shopid = app.globalData.shopId;
    var addressid = this.data.address_id;
    var params = {
      spk: shopkey,
      shopId: shopid,
      addressId: addressid,
    }

    request.requestPost(url, params)
      .then(data => {
        console.log(data);
        var tradeId = data.tradeId;
        payutil.payByTradeIdAction(tradeId)
          .then(res => {
            wx.redirectTo({
              url: '../pay/paysuccess',
            });
          }).catch(msg => {
            wx.hideToast('支付失败');
            wx.navigateBack({});
          })
      }).catch(msg => {
        console.log(msg);
      });
  },

  /**
   * 创建活动订单
   */
  createActivityTrade: function () {
    var url = config.activityOrderCreateUrl;
    var shopid = app.globalData.shopId;
    var addressid = this.data.address_id;
    var params = {
      activityId: this.data.activityId,
      shopId: shopid,
      addressId: addressid,
    }
    request.requestPost(url , params)
    .then(data => {
      console.log(data);
      var tradeId = data.tradeId;
      payutil.payByTradeIdAction(tradeId)
        .then(res => {
          wx.redirectTo({
            url: '../pay/paysuccess',
          });
        }).catch(msg => {
          wx.hideToast('支付失败');
          wx.navigateBack({});
        })
    }).catch(msg => {
      wx.showToast({
        title: msg,
        icon:'none'
      })
      console.log(msg);
    });

  },

  settleAction: function () {
    if (this.data.activityId) {
      this.createActivityTrade();
    } else {
      this.createTrade();
    }

  },

  /**
   *  获取地址列表
   **/
  getAddressLst: function () {
    var url = config.addressLstUrl;
    var params = {};
    request.requestPost(url, params)
      .then(data => {

        this.setData({
          addresslist: data
        });

        data.map(item => {
          if (item.isprimary == '1') {
            this.setData({
              address: item,
              address_id: item.id
            });
          }
        });

      }).catch(msg => {
        console.log(msg);
      });
  },

})